package com.young.dynamicexcel.configuration;

import com.young.dynamicexcel.common.util.SpringContextUtil;
import com.young.dynamicexcel.controller.DynamicExcelController;
import com.young.dynamicexcel.service.DynamicExcelService;
import com.young.dynamicexcel.service.impl.DynamicExcelServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @DESCRIPTION: 动态excel自动装配类
 * @USER: Young
 * @DATE: 2022/7/26 9:24
 */

@Configuration
@ConditionalOnClass(name = {"com.baomidou.mybatisplus.extension.service.impl.ServiceImpl"})
public class DynamicExcelAutoConfiguration {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public DynamicExcelService dynamicExcelService(){
        return new DynamicExcelServiceImpl();
    }

    @Bean
    @ConditionalOnBean(name = {"dynamicExcelService"})
    public DynamicExcelController dynamicExcelController(){
        return new DynamicExcelController();
    }

    @PostConstruct
    public void setSpringContext(){
        SpringContextUtil.setApplicationContext(applicationContext);
    }

}
